<template>
	<view class="register">
			<view class="title">{{bind==1?'为了体验肥猫完整服务请绑定您的手机号码':''}}</view>
			<form class="form">
				<view class="phone"><input type="text" v-model="phone" placeholder="请输入您的手机号" placeholder-style="fontSize: 30rpx;fontWeight: 400;color: #9DA5A8;" /></view>
				<view class="codeNum">
					<input class="codeNum" type="text" v-model="codeNum" placeholder="请输入验证码" placeholder-style="fontSize: 30rpx;fontWeight: 400;color: #9DA5A8;" />
					<button class="button" type="default" @click="code">{{ code1 == 0 ? time : '发送' }}</button>
				</view>
				<view class="agreement">
					<view class="text" v-if="bind==1">
						点击完成绑定按钮代表您已同意
						<text @click="$linkTo('/pages/mine/mine/setup/agreement')">《肥猫用户协议》</text>
					</view>
					<view class="text" v-else>
						点击登录按钮代表您已同意
						<text @click="$linkTo('/pages/mine/mine/setup/agreement')">《肥猫用户协议》</text>
						未注册的用户将直接注册并登陆
					</view>
				</view>
				<view class="button"><button type="warn" @click="handlePasswordLoginSubmit">{{bind==1?'完成绑定':'登陆/注册'}}</button></view>
			</form>
	</view>
</template>

<script>
	import md5 from '@/common/api/md5.js';
	import {requestUrl} from '@/common/config.js'
	export default {
		data() {
			return {
				phone: '13023779780',
				codeNum: '',
				code1: 1,
				access_token:'',
				Code:'',
				time:60,
				bind:''
			};
		},
		onShow() {

		},
		onLoad(option) {
			this.bind=option.id
		},
		methods:{
			code() {
				let _this = this;
				let time = Math.round(new Date().getTime() / 1000).toString();
				let Time = ['time', time].join('=');
				let Access_token = ['access_token', 'de8af75851bbe8bdbb1676db67418985b556f37d'].join('=');
				let content = ['content=pinet.FeiMao.2017'].join();
				let arr1 = [content, Time].sort(function(a, b) {
					a - b;
				});
				let newarr = md5(arr1.join('&'));
				if(this.code1==1){
					uni.request({
						url: requestUrl+'/app/user/sendcode',
						method: 'GET',
						data: {
							time: time,
							phone: this.phone,
							type: '0',
							sign: newarr
						},
						success: function(res) {
							uni.showToast({
								title:res.data.message,
								icon:'none'
							})
							if (res.data.errcode == 0) {
								_this.code1 = 0;
								let Time=setInterval(function(){
									_this.time=_this.time-1
									if(_this.time==0){
										clearInterval(Time)
										_this.time=60
										_this.code1 = 1;
									}
								},1000)
							}
						}
					});
				}else{
					uni.showToast({
						title:'操作频繁',
						icon:'none'
					})
				}
			},
			handlePasswordLoginSubmit() {
				let _this = this;
				if (!this.phone) {
					return uni.showToast({
						title: '请填写手机号',
						icon:'none'
					});
				}
				if (!this.codeNum) {
					return uni.showToast({
						title: '请输入验证码',
						icon:'none'
					});
				}
				let time = Math.round(new Date().getTime() / 1000).toString();
				let Time = ['time', time].join('=');
				let Access_token = ['access_token', 'de8af75851bbe8bdbb1676db67418985b556f37d'].join('=');
				let user=JSON.parse(uni.getStorageSync('user_info'))
				let content = ['content=pinet.FeiMao.2017'].join();
				let arr1 = [content, Time].sort(function(a, b) {
					a - b;
				});
				let newarr = md5(arr1.join('&'));
				if(this.bind==1){
					uni.request({
						url: requestUrl+'/app/user/oauthRegister',
						method: 'GET',
						data: {
							time: time,
							sign: newarr,
							oauth:'weixin',
							type:'6',
							city:user.city,
							sex:user.gender,
							headimgurl:user.headimgurl,
							language:user.language,
							nickname:user.nickName,
							openid:user.openId,
							province:user.province,
							unionId:user.unionId,
							country:user.country,
							phone: this.phone,
							verify_code: this.codeNum,
						},
						success: function(res) {
							console.log(res);
							uni.showToast({
								title:res.data.message,
								icon:'none'
							})
							if (res.data.errcode == 0) {
								uni.setStorage({
									key: 'access_token',
									data: JSON.stringify(res.data.content.access_token),
									success: function() {}
								});
								uni.setStorage({
									key: 'customer_id',
									data: JSON.stringify(res.data.content.customer_id),
									success: function() {}
								});
								uni.showToast({
									title:res.data.message,
									icon:'none'
								})
								uni.switchTab({
									url: '/pages/home/home/home'
								});
							}
						}
					});
				}else{
					this.$request('/app/user/Login', 'post', {
						phone: this.phone,
						code: this.codeNum,
					}).then(res => {
						if (res.errcode == 0) {
							uni.setStorage({
								key: 'access_token',
								data: JSON.stringify(res.content.access_token),
								success: function() {}
							});
							uni.setStorage({
								key: 'customer_id',
								data: JSON.stringify(res.content.customer_id),
								success: function() {}
							});
							uni.showToast({
								title: res.message,
								icon: 'none'
							});
							uni.switchTab({
								url: '/pages/home/home/home'
							});
						}
					});
				}
			},
		}
	}
</script>

<style lang="less">
	.register{
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;

			.title{
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #111111;
				padding: 60rpx 0;
			}
			.form {
						width: 100%;
				.agreement {
					padding-top: 80rpx;
					width: 450rpx;
					margin: 0 auto;
					margin-bottom: 60rpx;
					.text {
						font-size: 20rpx;
						font-weight: 400;
						color: #9da5a8;
						text {
							color: #00609b;
						}
					}
				}
				.phone {
					width: 90%;
					margin: 0 auto;
					border-bottom: 1px solid #f5f5f5;
					height: 100rpx;
					background: url(@/static/Login_phone@2x.png) no-repeat 6rpx 30rpx;
					background-size: 36rpx 42rpx;
					display: flex;
					align-items: center;
					input {
						padding-left: 60rpx;
						box-sizing: border-box;
					}
				}
				.codeNum {
					width: 90%;
					margin: 0 auto;
					border-bottom: 1px solid #f5f5f5;
					display: flex;
					align-items: center;
					background: url(@/static/Login_code@2x.png) no-repeat 6rpx 30rpx;
					background-size: 36rpx 42rpx;
					input {
						flex: 1;
						height: 100rpx;
						padding-left: 60rpx;
						box-sizing: border-box;
					}
					.button {
						width: 160rpx;
						height: 60rpx;
						font-size: 30rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #DB1B22;
						line-height: 60rpx;
						border-radius: 15px;
						border: 1px solid #DB1B22;
					}
				}
				.button {
					width: 90%;
					margin: 0 auto;
					button{
						border-radius: 20px;
					}
				}
			}
	}
</style>
